package by.yakusekmaxim.bookgid.webapp.page.home.book;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Book_;
import by.yakusekmaxim.bookgid.services.BookService;

public class BookListPanel extends Panel {

	@Inject
	BookService bookService;


	
	public BookListPanel(String id, final Filter<Book> filter) {
		super(id);

		BookDataProvider bookDataProvider = new BookDataProvider(filter);

		DataView<Book> dataView = new DataView<Book>("list", bookDataProvider,
				10) {
			@Override
			protected void populateItem(Item<Book> item) {
				final Book book = item.getModelObject();

				Label imageLabel = new Label("image", "");
				imageLabel.add(AttributeModifier.replace("src", book.getImage()
						.getUrl()));
				imageLabel.add(AttributeModifier.replace("alt", book.getImage()
						.getName()));
				imageLabel.add(AttributeModifier.replace("title", book
						.getImage().getName()));
				item.add(imageLabel);
				
				Link<Object> linkBook = new Link<Object>("linkBook") {
					@Override
					public void onClick() {

						setResponsePage(new BookDetailsPage(book));

					}
				};
				linkBook.add(new Label("name"));
				item.add(linkBook);
				item.add(new Label("nameOr"));
				item.add(new Label("date", getDataFormat(book.getDate())));
				item.add(new Label("author", getAuthorName(book)));
				item.add(new Label("genre", getGenreName(book)));

			}

		};

		add(dataView);

		add(new OrderByBorder<SingularAttribute<Book, ?>>("sortByName",
				Book_.name, bookDataProvider));
		add(new OrderByBorder<SingularAttribute<Book, ?>>("sortByDate",
				Book_.date, bookDataProvider));
		
		add(new PagingNavigator("paging", dataView));
		
	}

	private class BookDataProvider extends
			SortableDataProvider<Book, SingularAttribute<Book, ?>> {

		private Filter<Book> filter = new Filter<Book>();

		public BookDataProvider(Filter<Book> filter) {
			super();
			setSort(Book_.name, SortOrder.ASCENDING);
			this.filter = filter;

		}

		@Override
		public Iterator<? extends Book> iterator(long first, long count) {

			SingularAttribute<Book, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(
					sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);

			if (filter.getEnabled()) {
				return bookService.getAllPublicBook(sortParam, ascending,
						(int) first, (int) count, filter).iterator();
			}
			return bookService.getAllPublicBook(sortParam, ascending, (int) first,
					(int) count).iterator();
		}

		@Override
		public long size() {
			if (filter.getEnabled()) {
				return bookService.getCount(filter.getAttribute(),
						filter.getValue());
			}
			return bookService.getCount();
		}

		@Override
		public IModel<Book> model(Book book) {
			return new CompoundPropertyModel<Book>(book);
		}
	}

	private String getDataFormat(Date date) {
		if (date == null) {
			return "-";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
		return dateFormat.format(date);
	}

	private String getAuthorName(Book book) {
		if (book.getAuthor() == null) {
			return "-";
		}
		return book.getAuthor().getName();
	}

	private String getGenreName(Book book) {
		if (book.getGenre() == null) {
			return "-";
		}
		return book.getGenre().getName();
	}

}
